from django.db import models

from apps.users.models import User


class Image(models.Model):
    name = models.CharField(max_length=50, verbose_name='图片名称')
    url = models.CharField(max_length=100, verbose_name='图片的七牛云地址')

    class Meta:
        db_table = 'tb_image'
        verbose_name = '图片名称'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Category(models.Model):
    name = models.CharField(max_length=10, verbose_name='分类名称')

    class Meta:
        db_table = 'tb_category'
        verbose_name = '详情数据'
        verbose_name_plural = verbose_name


class Blog(models.Model):
    title = models.CharField(max_length=100, verbose_name='标题')
    abstract = models.CharField(max_length=100, verbose_name='摘要')
    content = models.CharField(max_length=10000, verbose_name='内容详情')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    category = models.ForeignKey(Category, verbose_name='分类名字', on_delete=models.SET_NULL, null=True)
    author = models.CharField(max_length=10, verbose_name='作者')
    read_count = models.IntegerField(verbose_name='浏览量', default=0)
    comment_count = models.IntegerField(verbose_name='评论数量', default=0)
    image = models.ForeignKey(Image, verbose_name='图片', default='', on_delete=models.SET_NULL, null=True)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, verbose_name='用户')
    is_recommend = models.BooleanField(verbose_name='推荐', default=False)

    class Meta:
        db_table = 'tb_detail'
        verbose_name = '详情数据'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Comment(models.Model):
    content = models.CharField(max_length=100, verbose_name='评论内容')
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, verbose_name='用户')
    blog = models.ForeignKey(Blog, related_name='comment', on_delete=models.SET_NULL, null=True, verbose_name='博客')
    comment_id = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, verbose_name='评论的id')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'tb_comment'
        verbose_name = '评论'
        verbose_name_plural = verbose_name
